package org.example.coffeeshoporderingsystem.member.controller;

import javafx.beans.InvalidationListener;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import org.example.coffeeshoporderingsystem.common.controller.BaseController;
import org.example.coffeeshoporderingsystem.common.ui.UIManager;
import org.example.coffeeshoporderingsystem.common.util.SpringContextUtil;
import org.example.coffeeshoporderingsystem.member.dto.MemberDTO;
import org.example.coffeeshoporderingsystem.member.entity.Member;
import org.example.coffeeshoporderingsystem.member.service.MemberService;
import org.example.coffeeshoporderingsystem.member.util.MemberUtil;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class MemberController extends BaseController {
    public TableView<Member> memberTableView;
    public TableColumn<Member,Integer> memberIdTabCol;
    public TableColumn<Member,String> nameTabCol;
    public TableColumn<Member,String> phoneTabCol;
    public TableColumn<Member,String> createAtTabCol;
    public TableColumn<Member, HBox> opeTabCol;
    // 分页控件
    public Pagination memberPagination;
    private MemberService memberService = SpringContextUtil.getBean(MemberService.class) ;
    private ObservableList<Member> obervableList = FXCollections.observableArrayList();
    // 当前页码
    private int pageNo = 1 ;
    public void initialize(URL url, ResourceBundle resourceBundle) {
        memberTableView.setItems(obervableList);

        memberIdTabCol.setCellValueFactory(new PropertyValueFactory<>("memberId"));
        nameTabCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        phoneTabCol.setCellValueFactory(new PropertyValueFactory<>("phoneNumber"));
        createAtTabCol.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getCreatedAt().toString()));
        opeTabCol.setCellValueFactory(cellData->{
            return new ObjectProperty<HBox>() {
                HBox hbox = new HBox() ;
                @Override
                public void bind(ObservableValue<? extends HBox> observableValue) {

                }

                @Override
                public void unbind() {

                }

                @Override
                public boolean isBound() {
                    return false;
                }

                @Override
                public Object getBean() {
                    return null;
                }

                @Override
                public String getName() {
                    return "";
                }

                @Override
                public HBox get() {
                    var selectBtn = new Button("选择");
                    var viewBtn = new Button("修改");
                    var delBtn = new Button("删除") ;
                    hbox.getChildren().addAll(selectBtn,viewBtn,delBtn);
                    selectBtn.setOnAction(event -> {
                        try {
                            var member = cellData.getValue();
                            System.out.println("选择会员:"+member);
                            MemberUtil.setMember(member) ;
                            // 跳转到 点餐页面
                            UIManager.switchToOrderList();
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });
                    return hbox;
                }

                @Override
                public void addListener(ChangeListener<? super HBox> changeListener) {

                }

                @Override
                public void removeListener(ChangeListener<? super HBox> changeListener) {

                }

                @Override
                public void addListener(InvalidationListener invalidationListener) {

                }

                @Override
                public void removeListener(InvalidationListener invalidationListener) {

                }

                @Override
                public void set(HBox hBox) {

                }
            };
        });
        this.loadMember();
    }

    public void loadMember() {
        var memberDTO = new MemberDTO();
        var memberPageInfo = memberService.findPageByMemberDTO(pageNo,memberDTO);
        // 设置当前页码
        memberPagination.setCurrentPageIndex(pageNo-1);
        // 设置总页数
        memberPagination.setPageCount(memberPageInfo.getPages());
        // 设置当前页数据
        memberPagination.setPageFactory(this::changePage);
        // 触发 分页控件
        changePage(1);
    }

    public TableView<Member> changePage(int pageNo) {
        obervableList.clear();
        // 封装查询条件
        var memberDTO = new MemberDTO();
        var memberPageInfo = memberService.findPageByMemberDTO(pageNo,memberDTO);
        obervableList.addAll(memberPageInfo.getList());
        return memberTableView;
    }
}
